package com.itheima.wechat;


import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.itheima.entity.Result;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.SmsUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {


    @Autowired
    JedisUtil jedisUtil;

    @Autowired//图片验证码，用于短信发送防刷
    DefaultKaptcha defaultKaptcha;



    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //调用工具发送
//        SmsUtil.sendSmsCode(telephone,String.valueOf(code));
        //把验证码存入redis
        jedisUtil.setex("002" + telephone,60 * 500,String.valueOf(code));
        return Result.success("");
    }


    @RequestMapping("send4Order")
    public Result send4Order(String telephone,String imgCode,String deviceId){
        String text = jedisUtil.get(deviceId);

        if(text==null || !text.equals(imgCode)){
            jedisUtil.del(deviceId);
            return Result.error("图形验证码错误，短信发送失败");
        }

        Long del = jedisUtil.del(deviceId);

        if(del == 0){
            return Result.error("请勿重复提交");
        }

        Integer code = ValidateCodeUtils.generateValidateCode(4);
        SmsUtil.sendSmsCode(telephone,code.toString());
        //把验证码存入redis 用于用户提交信息后的验证码比对
        //设置定时销毁，5分钟后自然销毁
        jedisUtil.setex("order"+telephone,300 * 10,code.toString());

        return Result.success("发送成功");

    }


    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis
        jedisUtil.setex(deviceId,60*5,text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }




}
